# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Manager Role

The manager role is responsible for accepting job requests and
distributing the individual tasks in the job to various worker
agents for execution.
"""

import yaml

from kestrel.core.datatypes import Worker, Job, Task
from kestrel.manager import WorkerPool, JobQueue, Policy, Interface


class ManagerRole(object):
    """
    The manager role is responsible for accepting job requests and
    distributing the individual tasks in the job to various worker
    agents for execution.

    Configuration entries for the modules in this role:
      manager/policy
      manager/interface

    Events raised by this module:
      manager/start
      manager/end
    """
    def __init__(self):
        """
        Set the name of the role and define the default module
        implementations.
        """
        self.name = 'manager'
        self.defaults = {'manager/policy': Policy,
                         'manager/interface': Interface,
                         'manager/queue': JobQueue,
                         'manager/pool': WorkerPool}
        self.ordering = ['manager/policy',
                         'manager/interface',
                         'manager/pool',
                         'manager/queue']
        self.modules = {}


    def register(self, kernel):
        """
        Instantiate and register the role's modules. The actual
        module implementations can be swapped by setting the
        appropriate configuration entries.
        """
        for module in self.ordering:
            impl = kernel.config(module, self.defaults[module])
            self.modules[module] = impl(kernel)
        kernel.event('manager/start')


    def unregister(self, kernel):
        """Unregister the role's modules from the kernel."""
        kernel.event('manager/end')
